﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      CIS  Overseas AreaManagement
// Created By:     Samara Jiang (samaraj@wicresoft.com)
// Created Date:   2011-1-27
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASB.CC.Business.CCS;
using ASB.CC.Business.CIS;
using ASBCC.Business;
using ASBCC.Business.CIS;
using ASBCC.Database.EntityFramework;

namespace BEGDC.CC.Web.CIS2.Admin
{
    /// <summary>
    /// CIS  Overseas AreaManagement
    /// </summary>
    public partial class AreaManagement : EntityPage
    {

        protected void Page_Load(object sender, EventArgs e)
        {


        }
        protected IEnumerable GetModules()
        {
            return Enum.GetNames(typeof(DepartmentModule));
        }

        protected IEnumerable GetModules(object data)
        {
            return Enum.GetNames(typeof(DepartmentModule)).Select(p => new
            {
                Letter = p,
                Data = data,
            });
        }

        protected IEnumerable GetCountryRelation(object dataItem, object letter)
        {
            string lett = letter as string;

            using (var dc = new CCEntities())
            {
                var raw = from t in dc.Countries
                          where (t.Disabled == false) && ((t.Name).Substring(0, 1) == lett.Substring(0, 1) || (t.Name).Substring(0, 1) == lett.Substring(1, 1))
                          select t;

                if (dataItem == null)
                {
                    return raw.ToArray();
                }
                else
                {
                    var countries = dataItem as IEnumerable<ASBCC.Database.EntityFramework.Country>;
                    var ids = countries.Select(p => p.CountryID);
                    var source = from t in raw.ToArray()
                                 select new
                                 {
                                     t.CountryID,
                                     t.Name,
                                     t.Description,
                                     Checked = ids.Contains(t.CountryID) ? "checked" : "",
                                 };
                    return source.ToArray();
                }
            }

        }
        protected override EntityDataSource EntitySource
        {
            get { return this.edsArea; }
        }

        protected override TextBox EntitySearchTxt
        {
            get { return this.SearchText; }
        }

        protected override Button EntityAddBtn
        {
            get { return this.BtnAdd; }
        }

        protected override Button EntitySearchBtn
        {
            get { return this.Searchbtn; }
        }
        protected override Button EntityClearBtn
        {
            get { return this.Cleanbtn; }
        }

        protected override GridView EntityGrid
        {
            get { return this.gvArea; }
        }

        protected override DetailsView EntityDetails
        {
            get { return this.dvArea; }
        }

        protected override void OnEntityDisabling(int entityKey)
        {
            AreaManagementHelper.SetAreaDisabled(entityKey);
            GlobalHelper.LogOperation(this.User.Identity.Name, SystemModule.CIO, "Area disabled. id=" + entityKey + "; name=" + AreaManagementHelper.getName(entityKey));
        }

        protected override string GetEntityWhereExpression(string keyword)
        {
            return "(it.Name like N'%" + keyword + "%'"
                 + " or it.Description like N'%" + keyword + "%')";
        }

        protected override void OnEntityInserting(EntityDataSourceChangingEventArgs e)
        {
            var dc = e.Context;
            var area = e.Entity as Area;
            var countryList = this.Request.Form["countrycheck"];

            AreaManagementHelper.InternalUpdateArea(dc, area, countryList);
        }

        protected override void OnEntityUpdating(EntityDataSourceChangingEventArgs e)
        {
            var dc = e.Context;
            var area = e.Entity as Area;
            area.Countries.Load();
            area.Countries.Clear();

            var countryList = this.Request.Form["countrycheck"];
            AreaManagementHelper.InternalUpdateArea(dc, area, countryList);
        }


        protected void edsArea_Inserted(object sender, EntityDataSourceChangedEventArgs e)
        {
            var dc = e.Context;
            var area = e.Entity as Area;
            var counties = LogInfoHelper.GetCountryWithArea(area.AreaID);
            GlobalHelper.LogOperation(this.User.Identity.Name, SystemModule.CIO, "Sales Region inserted. id=" + area.AreaID + "; Region=" + area.Name + "; Chinese Name=" + area.Description + "; country=" + counties);
        }

        protected void edsArea_Updated(object sender, EntityDataSourceChangedEventArgs e)
        {
            var dc = e.Context;
            var area = e.Entity as Area;
            var counties = LogInfoHelper.GetCountryWithArea(area.AreaID);
            GlobalHelper.LogOperation(this.User.Identity.Name, SystemModule.CIO, "Sales Region updated. id=" + area.AreaID + "; Region=" + area.Name + "; Chinese Name=" + area.Description + "; country=" + counties);
        }



    }
}
